package com.comac.print.component.app.utils;

import cn.hutool.core.util.ObjectUtil;
import com.comac.print.component.app.config.CustomerSplitCharacters;
import com.comac.print.component.app.domain.gateway.constant.CommonConstant;
import com.comac.print.component.app.domain.gateway.enums.IsRotateEnum;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.IBlockElement;
import com.itextpdf.layout.element.IElement;
import com.itextpdf.layout.property.Property;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

/**
 * @author yy
 * @description/note pdf工具类
 * @descriptionDetail/noteDetail pdf工具类
 * @createDate 2022/4/13
 **/
@Slf4j
public class ItextPdfUtils {

    private static ConverterProperties props;


    static {
        DefaultFontProvider fontProvider = new DefaultFontProvider();
        try {
            PdfFont ttcFont = PdfFontFactory.createTtcFont("/font/simsun.ttc", 1,
                    PdfEncodings.IDENTITY_H, false, true);
            fontProvider.addFont(ttcFont.getFontProgram());
        } catch (IOException e) {
            e.printStackTrace();
        }
        props = new ConverterProperties();
        props.setFontProvider(fontProvider);
    }

    /**
     * 生成竖版文书
     *
     * @param htmlStr
     * @return
     * @throws Exception
     */
    public static ByteArrayOutputStream generate(String htmlStr) throws Exception {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        HtmlConverter.convertToPdf(htmlStr, byteArrayOutputStream, props);
        return byteArrayOutputStream;
    }

    /**
     * 生成横板文书
     *
     * @param htmlStr
     * @return
     * @throws Exception
     */
    public static ByteArrayOutputStream generateHorizontal(String htmlStr,String systemId, String bussinessId) throws Exception {

        Document document=null;
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();

        try{
            PdfDocument pdf=new PdfDocument(new PdfWriter(byteArrayOutputStream));
            PageSize pageSize;

            IsRotateEnum isRotateEnum = IsRotateEnum.getIsRotateEnumByCode(systemId);
//            if (ObjectUtil.isNotNull(isRotateEnum) && isRotateEnum.isDescription()) {
//                pageSize = new PageSize(PageSize.A2.rotate());
//            } else {
//                pageSize = new PageSize(PageSize.A4);
//            }
            if (StringUtils.isNotBlank(bussinessId) && StringUtils.isNotBlank(systemId)){
                if (StringUtils.equals(systemId, CommonConstant.SYSTEM_ID_FO) && StringUtils.equals(bussinessId,CommonConstant.FO_INFO_FILE)){
                    pageSize = new PageSize(PageSize.A2.rotate());
                } else if (StringUtils.equals(systemId, CommonConstant.SYSTEM_ID_AOSR)){
                    pageSize = new PageSize(PageSize.A2.rotate());
                } else{
                    pageSize = new PageSize(PageSize.A4);
                }
            } else {
                pageSize = new PageSize(PageSize.A4);
            }
            document=new Document(pdf, pageSize);
            document.setProperty(Property.SPLIT_CHARACTERS, new CustomerSplitCharacters());
            List<IElement> iElements=HtmlConverter.convertToElements(htmlStr, props);
            for (IElement element : iElements) {
                document.add((IBlockElement) element);
            }
        }catch (Exception e){
            throw new RuntimeException("转化pdf失败", e);
        }finally {
            if(document !=null) {
                document.close();
            }
        }
        return byteArrayOutputStream;
    }

    /**
     * 根据html生成文书
     *
     * @param htmlContent
     * @param dataMap
     * @return
     * @throws IOException
     * @throws TemplateException
     */
    public static ByteArrayOutputStream generateByHTML(String htmlContent, Map<String, Object> dataMap) throws IOException, TemplateException {
        Configuration configuration = new Configuration();
        StringWriter out = new StringWriter();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        new Template("template", new StringReader(htmlContent), configuration).process(dataMap, out);
        HtmlConverter.convertToPdf(out.toString(), byteArrayOutputStream, props);
        return byteArrayOutputStream;
    }
}
